﻿using System.Windows.Forms;
using ExpertSystem.Interfaces;
using ExpertSystem.IViews;

namespace ExpertSystem.Views
{
    public partial class AskingVariableForm : Form, IAsckingVariableForm
    {
       
        private readonly IVariable _variable;

        public AskingVariableForm(IVariable variable = null)
        {
            InitializeComponent();
            iVariableBindingSource.DataSource = variable;
            bindingSource1.DataSource = variable;
            _variable = variable;
            CastomizeView();
        }

        private static void IntInputControl(object sender,KeyPressEventArgs keyEventArgs)
        {
            if (!char.IsNumber(keyEventArgs.KeyChar) && !char.IsControl(keyEventArgs.KeyChar))
            {
                keyEventArgs.Handled = true;
            }
        }

        private void CastomizeView()
        {
            if (_variable != null)
            {
                switch (_variable.ValueType)
                {
                    case "String":
                        {
                            if (_variable.PosibleValues.Count > 0)
                            {
                                panel1.Controls.Add(new ComboBox() { Parent = panel1,DataSource = bindingSource1 });
                                
                                
                            }
                            else
                            {
                                panel1.Controls.Add(new TextBox() { Parent = panel1 });
                            }
                            
                            
                            break;
                        }

                    case "Int":
                        {
                            var textbox = new TextBox() {Parent = panel1};
                            textbox.KeyPress += IntInputControl;
                            panel1.Controls.Add(textbox);
                        ;
                            break;
                        }

                }
            }
        }

       

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (_variable.PosibleValues.Count > 0)
            {
                _variable.Value = bindingSource1.Current;
            }
            else
            {
                var textbox = (TextBox) panel1.Controls[0];
                switch (_variable.ValueType)
                {
                    case "String":
                        {
                            _variable.Value = textbox.Text;
                            break;
                        }
                    case "Int":
                        {
                            int values = 0;
                            int.TryParse(textbox.Text, out values);
                            _variable.Value = values;
                            break;
                        }
                        
                }
                
            }
            if (_variable.Value.ToString().Length > 0)
            {
                DialogResult = DialogResult.OK;
            }
            
        }
    }
}
